package com.sinodata.bsm.cicp.collector.network;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import bsh.EvalError;
import bsh.Interpreter;

import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.ProtocolParameterUtil;
import com.sinodata.bsm.cicp.util.snmp.GetData;
import com.sinodata.bsm.common.constants.SnmpConstants;
import com.sinodata.bsm.common.utils.StringUtil;
import com.sinodata.bsm.common.vo.ProtocolParameter;
import com.sinodata.bsm.common.vo.ResPropertyValue;
import com.sinodata.bsm.common.vo.ResPropertyValue.PropValue;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-10-9 下午1:44:49          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class CPUMemCollector extends NetWorkCollector {

    private final static Logger logger = Logger.getLogger(CPUMemCollector.class);
    private final static String oidRegex = "\\.?(\\d+\\.)+\\d+";
    private final static Interpreter interpreter = new Interpreter();

    @Override
    public String[] getPropMeta() {
        return new String[] { "$1" };
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "sysoid", "type" };
    }

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        ProtocolParameter parameter = ProtocolParameterUtil.getSnmpValue(ip);
        String community = parameter.getUserName();
        Integer port = parameter.getPort();
        int version = SnmpConstants.getVersion(parameter.getProtocol());
        String sysoid = this.getParamMap().get("sysoid");
        int type = Integer.valueOf(this.getParamMap().get("type"));
        String oid = getOidBySysoid(sysoid, type);
        logger.info(ip + "  caiji wang luo shuju cpu and memory:sysoid[" + sysoid + "];type[" + type + "];oid[" + oid + "]");
        if (oid == null) {
            return null;
        }
        Map<String, String> oidMap = StringUtil.getAllByRegex(oid, oidRegex);
        Map<String, String> valueMap = new HashMap<String, String>(oidMap.size());
        String utilization = null;
        String[] propMeta = getPropMeta();
        try {
            for (String key : oidMap.keySet()) {
                if (key.equals("sum")) {
                    continue;
                }
                String value = GetData.snmpGet(ip, community, port, retries, timeout, version, oidMap.get(key));
                valueMap.put(key, value);
            }
            String expression = oidMap.get("sum");
            for (String key : valueMap.keySet()) {
                expression = expression.replace(key, valueMap.get(key));
            }
            logger.info("expression is [" + expression + "]");
            try {
                utilization = interpreter.eval(expression).toString();
            } catch (EvalError e) {
                throw new CollectorException("program occur error when bsh translate expression , " + expression + "", e);
            }
        } catch (Exception e) {
            logger.error("网络设备" + ip + "snmpGet出错，oid[" + oid + "]");
            throw new CollectorException("网络设备" + ip + "snmpGet出错，oid[" + oid + "]", e);
        }
        logger.info("utilization:" + utilization);
        List<ResPropertyValue> list = new ArrayList<ResPropertyValue>();
        ResPropertyValue resPropertyValueX = new ResPropertyValue();
        list.add(resPropertyValueX);
        PropValue[] props = new ResPropertyValue.PropValue[propMeta.length];
        props[0] = ResPropertyValue.createPropValue(utilization, propMeta[0]);
        resPropertyValueX.setValues(props);
        return list;
    }
}
